
//微信开放平台信息
var wxOpAppId = "wx15bc643e35c57f7d"
var wxOpAppSec = "95f2d9bafe0b65f4836366bd2dd26f29"
var wxMchId = "1598466121";

function wxLogin(func) {
    var wxPlus = api.require('wxPlus');
    wxPlus.auth({
        apiKey: wxOpAppId
        ,scope:"snsapi_userinfo"
    }, function(ret, err) {
        if (ret.status) {
            var code = ret.code
            wxPlus.getToken({
                apiKey: wxOpAppId,
                apiSecret: wxOpAppSec,
                code: code
            }, function(ret, err) {
                if (ret.status) {
                    var openId = ret.openId
                    var token = ret.accessToken
                    //通过OpenId获取用户
                    log(JSON.stringify(ret));
                    wxPlus.getUserInfo({
                        accessToken: token,
                        openId: openId
                    }, function(ret, err) {
                        if (ret.status) {
                            log(JSON.stringify(ret),"登录信息getUserInfo:");
                            var unionid = ret.unionid;
                            //此处进行登录
                            func(ret)
                        } else {
                            logJson(err.code);
                            showToast("系统繁忙请重试：ex03")
                        }
                    });


                } else {
                    logJson(err.code);
                    showToast("系统繁忙请重试：ex02")
                }
            });
            log(JSON.stringify(ret));
        } else {
            logJson(err);
            showToast("系统繁忙请重试：ex01")
        }
    });
}

function wxAuth(func) {
    var wxPlus = api.require('wxPlus');
    wxPlus.auth({
        apiKey: wxOpAppId
        ,scope:"snsapi_userinfo"
    }, function(ret, err) {
        if (ret.status) {
            var code = ret.code
            wxPlus.getToken({
                apiKey: wxOpAppId,
                apiSecret: wxOpAppSec,
                code: code
            }, function(ret, err) {
                if (ret.status) {
                    var openId = ret.openId
                    var token = ret.accessToken
                    func(openId,token)
                } else {
                    logJson(err.code);
                    showToast("系统繁忙请重试：ex02")
                }
            });
            log(JSON.stringify(ret));
        } else {
            logJson(err);
            // showToast("系统繁忙请重试：ex01")
            showToast("授权失败，请检查微信客户端")
        }
    });
}

function wxPay(param,func){
    var wxPay = api.require('wxPayPlus');
    wxPay.payOrder({
        apiKey: wxOpAppId,
        orderId: param.orderId,
        // mchId:  param.mchId,
        mchId:  wxMchId,
        nonceStr: param.nonceStr,
        timeStamp:  param.timeStamp,
        package:  param.package,
        sign: param.sign
    }, function(ret, err) {
        logJson(ret,"wxPay_ret: ")
        logJson(err,"wxPay_err: ")
        if (ret.status) {
            //支付成功
            func(ret)
        } else {
            showToast("支付取消")
            // alert(err.code);
        }
    });
}
